<?php
require_once("commom.inc.php");
if(!isset($_POST["submit"])){
    $username = $_POST["username"];
    $intro = $_POST["intro"];
    $haserror = false;
    $password = $_POST["password"];
    if(!$username) 
    {
    echo "用户名必须填写<br/>";
    $haserror = true;
    }else{
        $len = strlen($username);
        if($len<3){
            echo "用户名至少要有三个字符<br/>";
            $haserror = true;
        }
        else if($len >5){

            echo "用户名至多五个字符<br/>";
            $haserror = true;
        }else if(!preg_match('/^[a-zA-Z0-9]*$/',$username)){
            echo "格式不符合要求";
            $haserror = true;
        }
    }
    if(!$password)
    { 
    echo "密码必须填写<br/>";
    $haserror = true;
    }else{
        $len = strlen($password);
        if($len<3){
            echo "密码至少要有三个字符<br/>";
            $haserror = true;
        }
        else if($len >5){

            echo "密码至多五个字符<br/>";
            $haserror = true;
        }
    }
    $createTime = date_format(new DateTime(),"Y-m-d H:i:s");
    if(!$haserror){
        $sql = "INSERT INTO `looding` (`username`,`password`,`intro`)VALUES('$username','$password','$intro')";
        $conn = creatDb();
        $conn->query($sql);
        if($conn->ereor) die($conn->error); 
         $faceInfo = $_FILES["face"];
         $dest = __DIR__."/faces/$username.jpg";
         move_uploaded_file($faceInfo["tmp_name"],$dest);

    }else{
        $dest = __DIR__."/faces/$username.jpg";
        $src = __DIR__."/default-face.jpg";
        copy($src,$dest);
    }
    
}
?>
<?php require_once("header.inc.php"); ?>
<form  method="POST" action="register.php" enctype="multipart/form-data" class="dataset">
        
        <div class="field">
            <label>用户名</label><input type="text" name="username" value="" placeholder="请输入用户名">
        </div>
        <div class="field pwd">
        <label>密码</label><input type="password" name="password" title="至少需要四个字符" value=""/ >
        </div>
        <div class="field face">
        <label>头像</label><input type="file" name="face" title="上传图片"/ >
        </div>
       
        <div class="field">
        <label>自我介绍</label>
            <textarea name="intro" cols="80" rows="10">我叫sumerily
                我在四川
                </textarea>
        </div>
        <div class="actions">
            <input type="submit" value="注册"/>
            <input type="reset" value="重置" />
        </div>
         
    </form>
<?php require_once("footer.inc.php"); ?>